﻿using System;
using System.Collections;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;
using Project5188.BUS;
using Project5188.BUS.Operations;
using ExportToExcelTools;
using Project5188.Client.ClientCodes;

namespace Project5188.Client.Operations
{
    /// <summary>
    /// Interaction logic for CommunicationInfoPage.xaml
    /// </summary>
    public partial class CommunicationInfoPage
    {
        private CollectionViewSource _resources;
        private readonly long _personnelId;

        public CommunicationInfoPage()
            : base(Extensions.PageNames.CommunicationInfoPage)
        {
            InitializeComponent();

            pagingControl.DataChanged += PagingControlDataChanged;
        }

        public CommunicationInfoPage(long personnelId)
            : base(Extensions.PageNames.CommunicationInfoPage)
        {
            InitializeComponent();

            pagingControl.DataChanged += PagingControlDataChanged;

            _personnelId = personnelId;
        }

        protected override void OnPreviewTextInput(TextCompositionEventArgs e)
        {
            if (((FrameworkElement)(e.OriginalSource)).Name == txtRegistryNo.Name ||
                ((FrameworkElement)(e.OriginalSource)).Name == txtTCNo.Name)
            {
                e.Handled = !AreAllValidNumericChars(e.Text);
            }
            base.OnPreviewTextInput(e);
        }

        void PagingControlDataChanged(object sender, EventArgs e)
        {
            if (_resources != null) _resources.Source = pagingControl.Paging.GetDataList();
            if (_resources != null) dgCommunicationInfos.ItemsSource = _resources.Source as IEnumerable;
        }

        private void BtnAddClick(object sender, RoutedEventArgs e)
        {
            var insert = new InsertWindow();
            insert.Closed += InsertClosed;
            DetailView<communicationinfo> communicationInfoDetail = new CommunicationInfos();
            insert.SetEntity(communicationInfoDetail, Extensions.PageNames.CommunicationInfoPage.ToString(), _personnelId);
            insert.Show();
        }

        public override bool Search()
        {
            if (base.Search())
            {
                var communicationInfos = new CommunicationInfos();
                _resources = Resources["communicationInfoViewSource"] as CollectionViewSource;

                pagingControl.SetDataSource(communicationInfos.GetEntityListByParameters(txtRegistryNo.Text,
                                                                                         txtNameSurname.Text,
                                                                                         txtTCNo.Text,
                                                                                         dtBirthdate.SelectedDate), 1,
                                            10);
                if (_resources != null) _resources.Source = pagingControl.Paging.GetDataList();
                if (_resources != null) dgCommunicationInfos.ItemsSource = _resources.Source as IEnumerable;
            }
            else if (TransactionInfo.TransactionId.HasValue)
            {
                var communicationInfos = new CommunicationInfos();
                _resources = Resources["communicationInfoViewSource"] as CollectionViewSource;

                pagingControl.SetDataSource(communicationInfos.GetEntityById(TransactionInfo.TransactionId.Value), 1,
                                            10);
                if (_resources != null) _resources.Source = pagingControl.Paging.GetDataList();
                if (_resources != null) dgCommunicationInfos.ItemsSource = _resources.Source as IEnumerable;
            }

            return true;
        }

        private void BtnDetailClick(object sender, RoutedEventArgs e)
        {
            var communicationInfoValue = (communicationinfo)dgCommunicationInfos.SelectedItem;
            var detail = new DetailWindow();
            DetailView<communicationinfo> communicationInfoDetail = new CommunicationInfos { Entity = communicationInfoValue };
            detail.SetEntity(communicationInfoDetail, Extensions.PageNames.CommunicationInfoPage.ToString());
            detail.Show();
        }

        private void BtnUpdateClick(object sender, RoutedEventArgs e)
        {
            var communicationInfoValue = (communicationinfo)dgCommunicationInfos.SelectedItem;
            var update = new UpdateWindow();
            update.Closed += UpdateClosed;
            DetailView<communicationinfo> communicationInfoDetail = new CommunicationInfos();
            update.SetEntity(communicationInfoDetail, Extensions.PageNames.CommunicationInfoPage.ToString(), communicationInfoValue.a_ID);
            update.Show();
        }

        private void BtnDeleteClick(object sender, RoutedEventArgs e)
        {
            var communicationInfoValue = (communicationinfo)dgCommunicationInfos.SelectedItem;
            DetailView<communicationinfo> communicationInfoDetail = new CommunicationInfos();
            GenerateDeleteDialog(communicationInfoDetail, communicationInfoValue.a_ID);
        }

        private void BtnPrintClick(object sender, RoutedEventArgs e)
        {
            try
            {
                dgCommunicationInfos.ExportToExcel();
            }
            catch
            {
                MessageBox.Show("Excel'e aktarma sırasında hata oluştu.");
            }
        }
    }
}
